﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using DataRecv.models.road;
using System.IO;

namespace DataRecv
{
    public partial class addroad : Form
    {
        public addroad()
        {
            InitializeComponent();
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_id.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，路口编号不能为空";
                    return;
                }
                if (textBox_name.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，路口名称不能为空";
                    return;
                }
                if (textBox_gbdm.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，国标代码不能为空";
                    return;
                }
                if (textBox_kkpath.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，卡口文件夹不能为空";
                    return;
                }
                if (textBox_djpath.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，电警文件夹不能为空";
                    return;
                }
                if (textBox_bmbh.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，部门编号不能为空";
                    return;
                }
                if (textBox_xzqh.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，行政区划不能为空";
                    return;
                }
                if (textBox_name.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "添加失败，路口名称不能含有_";
                    return;
                }
                if (textBox_kkpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "添加失败，卡口文件夹不能含有_";
                    return;
                }
                if (textBox_djpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "添加失败，电警文件夹不能含有_";
                    return;
                }
                if (textBox_kkvpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "添加失败，卡口虚拟文件夹不能含有_";
                    return;
                }
                if (textBox_djvpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "添加失败，电警虚拟文件夹不能含有_";
                    return;
                }

                if (!Directory.Exists(textBox_kkpath.Text)) Directory.CreateDirectory(textBox_kkpath.Text);
                if (!Directory.Exists(textBox_djpath.Text)) Directory.CreateDirectory(textBox_djpath.Text);

                DataGridView dgv = ((road)(this.Owner)).dataGridView1;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    DataGridViewRow r = dgv.Rows[i];
                    if (textBox_id.Text == r.Cells["roadid"].Value.ToString())
                    {
                        toolStripStatusLabel1.Text = "添加失败，编号已存在";
                        return;
                    }
                }

                RoadParam param = new RoadParam();
                param.roadid = textBox_id.Text;
                param.gbdm = textBox_gbdm.Text;
                param.roadname = textBox_name.Text;
                param.kkpath = textBox_kkpath.Text;
                param.djpath = textBox_djpath.Text;
                param.kkvpath = textBox_kkvpath.Text;
                param.djvpath = textBox_djvpath.Text;
                param.bmbh = textBox_bmbh.Text;
                param.xzqh = textBox_xzqh.Text;
                param.bz = textBox_bz.Text;

                if (RoadParam.AddModel(param) == true)
                {
                    if (G.Roads != null)
                    {
                        Road r = new Road();
                        r.param = param;
                        G.Roads.Add(param.roadid, r);
                    }
                    dgv.Rows.Add(new object[] { textBox_id.Text, textBox_name.Text, textBox_gbdm.Text, textBox_kkpath.Text, textBox_djpath.Text, textBox_kkvpath.Text, textBox_djvpath.Text, textBox_bmbh.Text, textBox_xzqh.Text, textBox_bz.Text });
                    ((Form_Main)this.Owner.Owner).UpDateTree();
                    toolStripStatusLabel1.Text = "添加成功";
                }
                else
                {
                    toolStripStatusLabel1.Text = "添加失败";
                }
            }
            catch(Exception er) 
            {
                toolStripStatusLabel1.Text = er.Message;
            }
        }

        
        private void button_kkpath_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox_kkpath.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }

        private void button_djpath_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox_djpath.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }

    }
}
